<script>
import menus from "@/assets/js/menus.js";
import {utils} from "@/assets/util/index.js";

import FyDialog from "@/components/FyDialog.vue";

let util = utils();

let that = "";
export default {
  name: "Index",
  components: {FyDialog},
  data() {
    return {
      menus: menus,
      showUserBox: false,
      curYear: '',
      operators: [
        {
          id: 0,
          title: "首页",
          path: "/dashboard",
          checked: true,
          canClose: false
        }
      ],
      passwordVisible: false,
      userForm: {
        nickname: '',
        oldpassword: '',
        password: '',
        repassword: ''
      }
    }
  },
  created() {
    that = this;
    let operators = localStorage.getItem("operators");
    if (operators) {
      operators = JSON.parse(operators);
      this.operators = operators;
      operators.forEach(item => {
        if (item.checked) {
          this.checkMenuItem(item.path);
        }
      });
    }
    let curPath = localStorage.getItem("curPath");
    if (curPath) {
      this.checkMenuItem(curPath);
    }
    this.curYear = new Date().getFullYear();
  },
  methods: {
    extendItem(id, path) {
      let _menus = this.menus;
      let noSubmenu = false;
      _menus.forEach(item => {
        item.show = false;
        if (item.id === id) {
          item.show = true;
          if (!item.children || item.children.length < 1) {
            noSubmenu = true;
            item.show = false;
          }
        }
      });
      if (noSubmenu) {
        this.checkSubItem(id, "", path)
      }
    },
    checkSubItem(itemId, subitemId, path) {
      let _menus = this.menus;
      let obj;
      _menus.forEach(item => {
        item.checked = false;
        let children = item.children;
        children.forEach(subItem => {
          subItem.checked = false;
        });
        if (item.id === itemId) {
          if (subitemId) {
            children.forEach(subItem => {
              subItem.checked = false;
              if (subItem.id === subitemId) {
                subItem.checked = !subItem.checked;
                obj = subItem;
              }
            });
          } else {
            item.checked = !item.checked;
            obj = item;
          }
        }
      });

      this.$router.push({
        path: path
      })
      this.operators = util.handleOperators(this.operators, obj.title, path);
    },
    removeOperator(id) {
      let operators = this.operators;
      let index = 0;
      operators.forEach((item, idx) => {
        if (item.id === id) {
          index = idx;
        }
      });
      operators.splice(index, 1);
      let operator = operators[operators.length - 1];
      let path = operator.path;

      operators.forEach(item => {
        item.checked = item.path === path;
      });

      this.operators = operators;
      this.$router.push({
        path: path
      });
      this.checkMenuItem(path);
    },
    checkMenuItem(path) {
      let o;
      let so = null;
      let _menus = this.menus;
      _menus.forEach(item => {
        item.checked = false;
        let children = item.children;
        children.forEach(subItem => {
          subItem.checked = false;
          if (subItem.path === path) {
            o = item;
            so = subItem;
            item.show = true;
          }
        });
        if (item.path === path) {
          o = item;
        }
      });
      if (so) {
        this.checkSubItem(o.id, so.id, path);
      } else {
        this.extendItem(o.id, path);
      }
    },
    checkTab(path) {
      this.checkMenuItem(path);
    },
    navigatorUrl(title, path) {
      this.$router.push({
        path: path
      });
      this.operators = util.handleOperators(this.operators, title, path);
    }
  },
  mounted() {
    window.addEventListener("setItemEvent", function (ev) {
      if (ev.key === 'operators') {
        that.operators = JSON.parse(ev.newValue);
      }
    });
    window.addEventListener("removeItemEvent", function (ev) {
      if (ev.key === 'operators') {
        that.operators = JSON.parse(ev.newValue);
      }
    });
  }
}
</script>

<template>
  <div class="flex-rcs">
    <div class="leftMenu">
      <div class="leftMenuTitle">XueTang</div>
      <div v-for="item in menus" style="position:relative;">
        <div v-if="!item.hidden">
          <div class="leftMenuLine" v-if="item.show"></div>
          <div :class="'leftMenuItem menuitem flex-rcs '+(item.checked?'subitemActive not-hover':'')"
               @click="extendItem(item.id,item.path)"
               data-url="/resource"
               :style="item.show?'color:var(--xt-background-aphla);':''">
            <div>
              <label :class="'iconfont icon-' + item.icon "></label>
              <label>{{ item.title }}</label>
            </div>
            <label :class="'iconfont icon-'+item.leftIcon"></label>
          </div>
          <transition name="fade" appear>
            <div v-if="item.show" id="subitem_id">
              <div :class="'menuitem flex-rcs '+(subItem.checked?'subitemActive not-hover':'')"
                   v-for="subItem in item.children"
                   @click="checkSubItem(item.id, subItem.id,subItem.path)"
              >
                <div class="subItemTitle">{{ subItem.title }}</div>
                <label :class="'iconfont icon-'+subItem.leftIcon"></label>
              </div>
            </div>
          </transition>
        </div>
      </div>
    </div>
    <div class="right_box">
      <div class="right_header flex-rcs">
        <div></div>
        <div style="height:100%;">
          <div class="user_box">
            <div></div>
            <div>supostacks</div>
            <div class="user_bottom_box flex-cc">
              <div @click="navigatorUrl('个人中心','/personal')"><label class="iconfont icon-account"></label>个人中心
              </div>
              <div @click="passwordVisible=true"><label class="iconfont icon-key"></label>修改密码</div>
              <div class="line"></div>
              <div @click="exitSystem()"><label class="iconfont icon-tuichu"></label>退出登录</div>
            </div>
          </div>
        </div>
      </div>
      <div class="right_operator flex-rcs">
        <div class="left">
          <div><label class="iconfont icon-shangyishou"></label></div>
        </div>
        <div class="operator_content flex-rn">
          <div :class="'operator_title flex-rcs '+(item.checked?'operator_active':'')"
               v-for="item in operators">
            <div @click="checkTab(item.path)">{{ item.title }}</div>
            <label v-if="item.canClose" class="iconfont icon-shibai" @click="removeOperator(item.id)"></label>
          </div>
        </div>
        <div class="right flex-rcs">
          <div class="right_icon"><label class="iconfont icon-xiayishou"></label></div>
          <div class="right_refresh"><label class="iconfont icon-sync-alt"></label>刷新</div>
        </div>
      </div>
      <div class="right_main">
        <router-view></router-view>
      </div>
      <div class="right_footer">
        <label>© {{ curYear }} SupoStacks Copyright</label>
      </div>
    </div>

    <FyDialog @close="passwordVisible=false" :visible="passwordVisible" title="重置密码">
      <template #form>
        <el-form style="padding:15px;">
          <el-form-item label="用户名" :label-width="80">
            <el-input v-model="userForm.username" disabled></el-input>
          </el-form-item>
          <el-form-item label="原密码" :label-width="80">
            <el-input type="password" v-model="userForm.oldpassword"></el-input>
          </el-form-item>
          <el-form-item label="新密码" :label-width="80">
            <el-input type="password" v-model="userForm.password"></el-input>
          </el-form-item>
          <el-form-item label="确认密码" :label-width="80">
            <el-input type="password" v-model="userForm.repassword"></el-input>
          </el-form-item>
        </el-form>
      </template>
      <template #footer-btn>
        <div>
          <el-button class="submit_btn" @click="savePassword">确 定</el-button>
          <el-button type="danger" @click="passwordVisible = false">取 消</el-button>
        </div>
      </template>
    </FyDialog>
  </div>
</template>

<style lang="scss">
body {
  --left_menu_width: 20vw;
}

.leftMenu {
  width: var(--left_menu_width);
  max-height: 100vh;
  min-height: 100vh;
  box-shadow: #999 0 0 2px;
  background: rgb(249, 250, 252);
  color: #616161;
  font-weight: bold;
  z-index: 1000;
}

.leftMenuTitle {
  background: var(--xt-background);
  height: var(--xt-header-height);
  line-height: var(--xt-header-height);
  text-align: center;
  color: var(--xt-color);
}

.leftMenuItem {

  cursor: pointer;
  height: 55px !important;
  line-height: 55px !important;
  padding-left: 28px;
  padding-right: 18px;
  font-size: 15px;

  > div {
    .iconfont {
      margin-right: 4px;
    }
  }

  .icon-angle-right {
    font-size: 10px;
  }

  &:hover {
    background: rgb(240, 245, 255);
  }

}

.menuitem {
  height: 40px;
  line-height: 40px;
  font-size: 14px;
  font-weight: bold;

  &:not(.not-hover):hover {
    cursor: pointer;
    background: rgb(240, 245, 255);
  }

  &.not-hover:hover {
    cursor: pointer;
    background: var(--xt-background-aphla-0-2);
  }

  .subItemTitle {
    padding-left: 40px;
  }
}

.leftMenuLine {
  position: absolute;
  height: 100%;
  width: 4px;
  background: var(--xt-background-aphla-0-8);
}

.subitemActive {
  background-color: var(--xt-background-aphla-0-2);
  color: var(--xt-background-aphla-0-8);
}


.fade-enter-active, .fade-leave-active {
  transition: opacity .5s;
}

.fade-enter, .fade-leave-to /* .fade-leave-active for <=2.1.8 */
{
  opacity: 0;
}


.right_box {
  height: 100vh;
  width:calc(100vw - var(--left_menu_width));
  .right_header {
    height: var(--xt-header-height);
    background-color: var(--xt-background-aphla-0-8);
    padding-left: 20px;
  }

  .right_operator {
    height: 50px;
    width: calc(100vw - var(--left_menu_width));
    box-shadow: #ccc 0 0 2px;
    background-color: rgb(250, 250, 250);

    .left, .right {
      > div {
        text-align: center;
        height: 100%;
        line-height: 49px;
        background-color: #fff;

        &:hover {
          background-color: #eee;
        }
      }
    }

    .left {
      width: 35px;
      border-right: 1px solid #eee;
      cursor: pointer;

      > label {
        cursor: pointer;
      }
    }

    .operator_content {
      width: calc(100vw - var(--left_menu_width) - 35px - 120px);
      height: 100%;

      .operator_title {
        height: 100%;
        padding-left: 15px;
        padding-right: 15px;
        line-height: 50px;
        border-right: 1px solid #eee;
        width: fit-content;
        cursor: pointer;

        .icon-shibai {
          color: #ccc;
          cursor: pointer;
          margin-left: 4px;

          &:hover {
            color: #f00;
          }
        }
      }

      .operator_active {
        background-color: rgb(240, 245, 255);
      }
    }

    .right {
      width: 120px;

      .right_icon {
        width: 35px;
        background-color: #fff;
        border-right: 1px solid #eee;
        border-left: 1px solid #eee;
        cursor: pointer;

        > label {
          cursor: pointer;
        }
      }

      .right_refresh {

        background-color: #fff;
        width: 85px;
        text-align: center;

        > label {
          margin-right: 4px;
          cursor: pointer;
        }

        cursor: pointer;
      }
    }
  }

  .right_main {
    padding: 20px;
    height: calc(100vh - 90px - 30px - var(--xt-header-height));
    overflow-y: scroll;
  }

  .right_footer {
    line-height: 30px;
    font-size: 12px;
    height: 30px;
    background-color: #fff;
    position: absolute;
    width: calc(100vw - var(--left_menu_width));
    bottom: 0;

    > label {
      position: absolute;
      right: 20px;
    }
  }
}

.user_box {
  width: fit-content;
  height: 100%;
  line-height: var(--xt-header-height);
  padding-right: 20px;
  padding-left: 20px;
  color: #fff;
  font-weight: bold;

  &:hover {
    cursor: pointer;
    background-color: var(--xt-background-aphla-0-8);
  }

  &:hover {
    .user_bottom_box {
      display: block;
    }
  }
}

.user_bottom_box {
  display: none;
  position: absolute;
  right: 0;
  top: var(--xt-header-height);
  width: 13%;
  text-align: center;
  color: #333;
  font-weight: normal !important;
  box-shadow: #ccc 0 0 3px;
  background-color: #fff;

  > div {
    height: 40px;
    line-height: 40px;
    margin: 3px;
    border-radius: 3px;

    > label {
      margin-right: 5px;
    }
  }


  > div:hover {
    cursor: pointer;
    background-color: #f2f2f2;
  }

  .line {
    height: 1px;
    background-color: #f2f2f2;
  }
}

</style>
